package com.hazesoft.hdclib;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class HazeComponentManager {
	private static final String DEBUG_MESSAGE_TAG = "Haze Component Manager"; 
	private static final String LIBRARY_VERSION_STRING = "v1.2.1.2";
	
	private String mInstanceUUID;
	private String mInstanceLayoutPath;
	private Context mInstanceContext;
	private ScrollView mInstanceView;
	private SharedPreferences mInstanceSharedPreferences;
	private SharedPreferences.Editor mInstancePreferenceEditor;
	
	/**
	 * Initializes an instance of the HazeComponentManager with the Unique ID specified 
	 * in instanceUUID.
	 * @param instanceUUID This instance's Unique ID.
	 * @param instanceContext This instance's Android {@link Context#}
	 */
	public HazeComponentManager(String instanceUUID, Context instanceContext)
	{
		Log.i(DEBUG_MESSAGE_TAG, "New instance created. Instance UUID=" + instanceUUID);
		mInstanceUUID = instanceUUID;
		mInstanceContext = instanceContext;
		
		// Setup the path that the local layout file will be saved. The UUID is used so that
		// multiple instances of the HazeComponentManager can be used in the same application.
		mInstanceLayoutPath = mInstanceContext.getFilesDir() + "/" + mInstanceUUID + ".xml";
		
		// Create shared preferences that can be used for this instance of the HazeComponentManager
		mInstanceSharedPreferences = mInstanceContext.getSharedPreferences(mInstanceUUID, Context.MODE_PRIVATE);
		mInstancePreferenceEditor = mInstanceSharedPreferences.edit();
	}
	
	/**
	 * Write a shared preference in the context of this HazeComponentManager's instance.
	 * @param preferenceName The key of the shared preference.
	 * @param preferenceValue The value to be persisted.
	 */
	public void WritePreference(String preferenceName, String preferenceValue)
	{
		mInstancePreferenceEditor.putString(preferenceName, preferenceValue);
		mInstancePreferenceEditor.commit();
	}
	
	/**
	 * Get a shared preference in the context of this HazeComponentManager's instance.
	 * @param preferenceName The key of the shared preference.
	 * @param preferenceDefaultValue The default value of the shared preference that gets returned if a
	 * value does not exist for the specified {@link preferenceName}
	 * @return
	 */
	public String ReadPreference(String preferenceName, String preferenceDefaultValue)
	{
		return mInstanceSharedPreferences.getString(preferenceName, preferenceDefaultValue);
	}
	
	/**
	 * Download the layout from a web server using the built-in {@link HttpLayoutDownloader}.
	 * @param layoutUri The Uri of the remote file to be downloaded.
	 * @param forceDownload A value indicating whether the download should skip checking the current
	 * file version and re-download the layout file.
	 * @return Returns {@value true} if the download was successful.
	 */
	public boolean DownloadLayout(String layoutUri, boolean forceDownload)
	{
		HttpLayoutDownloader httpDownloader = new HttpLayoutDownloader();
		return DownloadLayout(httpDownloader, forceDownload, layoutUri);
	}
	
	public void DownloadLayoutAsync(String layoutUri, boolean forceDownload)
	{
		HttpLayoutDownloader httpDownloader = new HttpLayoutDownloader();
		DownloadLayoutAsync(httpDownloader, forceDownload, layoutUri);
	}
	
	/**
	 * Download the layout file using a custom downloader.
	 * @param customDownloader An instance of a custom downloader that extents the type {@link HazeLayoutDownloader}
	 * @param forceDownload A value indicating whether the download should skip checking the current
	 * file version and re-download the layout file.
	 * @param downloadParams The parameter passed to the downloader to indicate the location from which the file must
	 * be downloaded.
	 * @return
	 */
	public boolean DownloadLayout(HazeLayoutDownloader customDownloader, boolean forceDownload, Object downloadParams)
	{
		if(customDownloader == null)
			return false;
		
		customDownloader.Prepare(this, downloadParams);
		if(forceDownload)
		{
			return customDownloader.Download(mInstanceLayoutPath);
		}
		else
		{
			if(customDownloader.RequireDownload(mInstanceLayoutPath))
			{
				return customDownloader.Download(mInstanceLayoutPath);
			}
			else
			{
				return true;
			}
		}
	}
	
	public void DownloadLayoutAsync(HazeLayoutDownloader customDownloader, boolean forceDownload, Object downloadParams)
	{
		BackgoundLayoutDownloader backgroundDownloader = new BackgoundLayoutDownloader();
		backgroundDownloader.execute(customDownloader, forceDownload, downloadParams, this);
	}
	
	public String GetLayoutPath()
	{
		return mInstanceLayoutPath;
	}
	
	/**
	 * Build the {@link View} that contains all the dynamic components.
	 * @return The newly generated {@link View}
	 */
	public View GetView()
	{
		// If the main scroll view has not yet been initialized, initialize it.
		if(mInstanceView == null)
			mInstanceView = new ScrollView(mInstanceContext);
		
		// Remove any children from the main scroll view
		mInstanceView.removeAllViews();
		
		// Create a linear layout view to contain all the components.
		LinearLayout contentLinearLayout = new LinearLayout(mInstanceContext);
		
		// Set the linear layout's orientation to VERTICAL so that the
		// children act as list items.
		contentLinearLayout.setOrientation(LinearLayout.VERTICAL);
		
		// TODO: All all the controls to the linear layout.
		
		// Add text info to the end of the view to indicate the library
		// version as well include a banner.
		TextView textViewInfo = new TextView(mInstanceContext);
		textViewInfo.setText("Powered by HazeSoft's hdcLib");
		
		TextView textViewVersion = new TextView(mInstanceContext);
		textViewVersion.setText(LIBRARY_VERSION_STRING);
		
		contentLinearLayout.addView(textViewInfo);
		contentLinearLayout.addView(textViewVersion);
		
		// Add the linear layout to the main scroll view.
		mInstanceView.addView(contentLinearLayout);
		
		return mInstanceView;
	}
	
	
}
